package tt.dz.order.util.page;

import java.util.List;

import org.springframework.util.Assert;

import tt.dz.order.vo.BaseVo;

/**
 * 分页bean
 * 
 * @author anson-worker
 *
 * @param <T>
 */
public class Pagination<T> extends BaseVo implements Page {

	private List<T> list; // 要返回的某一页的记录列表
	private List<T> OrderItemlist; // 用于显示订单子项
	private int allRow; // 总记录数
	private int totalPage; // 总页数
	private int currentPage; // 当前页
	private int pageSize; // 每页记录数

	private boolean isFirstPage; // 是否为第一页
	private boolean isLastPage; // 是否为最后一页
	private boolean hasPreviousPage; // 是否有前一页
	private boolean hasNextPage; // 是否有下一页

	/**
	 * 计算当前页开始记录
	 * 
	 * @return 当前页开始记录号
	 */
	@Override
	public int countOffset() {
		return this.countOffset(currentPage, pageSize);
	}

	/**
	 * 设置当前页
	 * 
	 * @param currentPage
	 */
	@Override
	public void setCurrentPage(int currentPage) {
		Assert.isTrue(currentPage>0,"当前页小于0");
		this.currentPage = currentPage;
	}

	/**
	 * 设置每页显示多少条
	 * 
	 * @param pageSize
	 */
	@Override
	public void setPageSize(int pageSize) {
		Assert.isTrue(pageSize>0,"每页显示条数小于0");
		this.pageSize = pageSize;
	}

	/**
	 * 处理当前分页
	 */
	@Override
	public void dealPage() {
		this.isFirstPage = this.currentPage == this.getTopPageNo();
		this.isLastPage = this.currentPage == this.getBottomPageNo();
		this.hasNextPage = !this.isLastPage;
		this.hasPreviousPage = !this.isFirstPage;
		this.totalPage = this.getTotalPages();
	}

	/**
	 * @return 总页数
	 */
	@Override
	public int getTotalPages() {
		return (allRow + pageSize - 1) / pageSize;
	}

	/**
	 * @return 首页
	 */
	@Override
	public int getTopPageNo() {
		return 1;
	}

	/**
	 * @return 上一页
	 */
	@Override
	public int getPreviousPageNo() {
		if (currentPage <= 1) {
			return 1;
		}
		return currentPage - 1;
	}

	/**
	 * @return 下一页
	 */
	@Override
	public int getNextPageNo() {
		if (currentPage >= getBottomPageNo()) {
			return getBottomPageNo();
		}
		return currentPage + 1;
	}

	/**
	 * @return 尾页
	 */
	@Override
	public int getBottomPageNo() {
		return getTotalPages();
	}

	/**
	 * 计算当前页开始记录
	 * 
	 * @param pageSize
	 *            每页记录数
	 * @param currentPage
	 *            当前第几页
	 * @return 当前页开始记录号
	 */
	private int countOffset(int currentPage, int pageSize) {
		int offset = pageSize * (currentPage - 1);
		return offset;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getAllRow() {
		return allRow;
	}

	public void setAllRow(int allRow) {
		this.allRow = allRow;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public boolean isFirstPage() {
		return isFirstPage;
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	public boolean isLastPage() {
		return isLastPage;
	}

	public void setLastPage(boolean isLastPage) {
		this.isLastPage = isLastPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public List<T> getOrderItemlist() {
		return OrderItemlist;
	}

	public void setOrderItemlist(List<T> orderItemlist) {
		OrderItemlist = orderItemlist;
	}
	
}
